import { NodeConfig } from './nodeConfig.js'

/**
 * 拖拽管理器
 */
export class DragManager {
  constructor(graphManager) {
    this.graphManager = graphManager
    this.container = null
    this.bindEvents()
  }

  /**
   * 绑定拖拽事件
   */
  bindEvents() {
    this.container = document.getElementById('graph-container')
    
    this.container.addEventListener('dragover', this.handleDragOver.bind(this))
    this.container.addEventListener('dragleave', this.handleDragLeave.bind(this))
    this.container.addEventListener('drop', this.handleDrop.bind(this))
  }

  /**
   * 处理拖拽悬停
   */
  handleDragOver(e) {
    e.preventDefault()
    e.dataTransfer.dropEffect = 'copy'
    
    // 添加拖拽悬停时的视觉反馈
    this.container.style.borderColor = '#1890ff'
    this.container.style.borderWidth = '2px'
  }

  /**
   * 处理拖拽离开
   */
  handleDragLeave(e) {
    // 恢复容器样式
    this.container.style.borderColor = '#d9d9d9'
    this.container.style.borderWidth = '1px'
  }

  /**
   * 处理拖拽放置
   */
  handleDrop(e) {
    e.preventDefault()
    
    // 恢复容器样式
    this.container.style.borderColor = '#d9d9d9'
    this.container.style.borderWidth = '1px'
    
    try {
      // 检查 graph 对象是否存在
      if (!this.graphManager.graph) {
        console.error('Graph 对象未初始化')
        throw new Error('画布未初始化，请刷新页面重试')
      }
      
      const data = JSON.parse(e.dataTransfer.getData('text/plain'))
      const rect = this.container.getBoundingClientRect()
      
      // 计算相对于画布的位置
      const x = e.clientX - rect.left
      const y = e.clientY - rect.top
      
      // 考虑画布的缩放和平移
      const zoom = this.graphManager.graph.zoom()
      
      // 获取画布的变换信息
      let panX = 0, panY = 0
      try {
        // 在 X6 1.x 中，尝试从视图状态获取平移信息
        const view = this.graphManager.graph.view
        if (view && view.state) {
          panX = view.state.tx || 0
          panY = view.state.ty || 0
        } else if (this.graphManager.graph.getPan) {
          const pan = this.graphManager.graph.getPan()
          panX = pan.x || 0
          panY = pan.y || 0
        } else if (this.graphManager.graph.getTransform) {
          const transform = this.graphManager.graph.getTransform()
          panX = transform.tx || 0
          panY = transform.ty || 0
        }
        
        // 调试信息

      } catch (error) {
        console.warn('无法获取画布平移信息，使用默认值:', error)
      }
      
      // 计算实际位置：考虑缩放和平移
      let actualX, actualY
      
      // 使用 X6 的坐标转换 API
      if (this.graphManager.graph.clientToLocal) {
        const point = this.graphManager.graph.clientToLocal(e.clientX, e.clientY)
        actualX = point.x
        actualY = point.y
      } else {
        // 当画布没有平移时，直接使用鼠标位置
        if (panX === 0 && panY === 0) {
          actualX = x
          actualY = y
        } else {
          // 当画布有平移时，考虑平移和缩放
          actualX = (x - panX) / zoom
          actualY = (y - panY) / zoom
        }
      }
      
      // 获取节点尺寸并计算居中位置
      const size = NodeConfig.getNodeSize(data.type)
      actualX = actualX - size.width / 2
      actualY = actualY - size.height / 2
      
      
      
      // 创建节点
      this.graphManager.createNodeByType(data.type, data.label, actualX, actualY)
      
      return { success: true, data }
    } catch (error) {
      console.error('拖拽放置失败:', error)
      throw error
    }
  }

  /**
   * 解绑拖拽事件
   */
  unbindEvents() {
    if (this.container) {
      this.container.removeEventListener('dragover', this.handleDragOver.bind(this))
      this.container.removeEventListener('dragleave', this.handleDragLeave.bind(this))
      this.container.removeEventListener('drop', this.handleDrop.bind(this))
    }
  }

  /**
   * 销毁
   */
  dispose() {
    this.unbindEvents()
  }
} 